import { CreateRestRequest } from "../../../http";
import { RestHttpTask } from "../../../resttask";

export class GetRestTask extends RestHttpTask {
    constructor(apiAddress) {
        super();
        this.apiAddress = apiAddress;
    }
    registerRequest() {
        var self = this;
        return new AUIAuios().method(CreateRestRequest('GET', 
            self.apiAddress, self.params, {}));
    }
}

export class GetAllRestTask extends GetRestTask {
    constructor(apiAddress) {
        super(apiAddress);
        this.append = "";
    }
    getAddress(append) {
        return this.apiAddress + append;
    }
    registerRequest() {
        var self = this;
        return new AUIAuios().method(CreateRestRequest('GET', 
        self.getAddress(self.append), self.params, {}));
    }
}

export class GetSingleRestTask extends GetRestTask {
    constructor(apiAddress) {
        super(apiAddress);
        this.id = "";
    }
    getAddress(append) {
        return this.apiAddress + append;
    }
    registerRequest() {
        var self = this;
        if (self.id) {
            return new AUIAuios().method(CreateRestRequest('GET', 
                self.getAddress("/" + self.id), self.params, {}));
        } else {
            console.error("GetSingleRestTask no id");
        }
    }
}

export class PostRestTask extends RestHttpTask {
    constructor(apiAddress) {
        super();
        this.apiAddress = apiAddress;
        this.data = {};
    }
    registerRequest() {
        var self = this;
        self.myHeaders = new Headers();
        self.myHeaders.append('Accept', 'application/json, text/plain, */*');
        self.myHeaders.append('Content-Type', 'application/json');
        self.request = CreateRestRequest('POST', 
        self.apiAddress, self.params, {
            body: JSON.stringify(self.data),
            headers: self.myHeaders
        });
        return new AUIAuios().method(self.request);
    }
}